/**
 * 
 */
package registnet.courseDemand.extras;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * @author Dexter Ryan Carrasco
 */
public class RecommendedCoursesCutter {

	public static void main(final String args[]) {
		final File aFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/WRITEIN-2007-SECOND EDITED");
		final File outFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/CUT_WRITEIN_2007_DATA");

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					aFile));
			// use buffering
			final BufferedWriter output = new BufferedWriter(new FileWriter(
					outFile));

			try {
				String line = null; // not declared within while loop
				int allowed = 0;
				String tokens[];
				List<RecommendedElement> recommended = new ArrayList<RecommendedElement>();
				List<String> courses = new ArrayList<String>();

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {

					RecommendedCoursesCutter.sortElements(recommended);

					if (line.charAt(0) == '#') {

						int total = 0;
						for (final Iterator<RecommendedElement> elemIter = recommended
								.iterator(); elemIter.hasNext();) {
							final RecommendedElement element = elemIter.next();
							final int unit = element.getUnit();

							if (total + unit <= allowed) {
								total += unit;
								output.write(element.getStudentNumber() + ","
										+ element.getCourseCode() + ","
										+ element.getUnit());
								output.newLine();
							}
						}

						recommended = new ArrayList<RecommendedElement>();
						courses = new ArrayList<String>();
						continue;
					}

					tokens = line.split(",");
					allowed = Integer.parseInt(tokens[8]);
					final RecommendedElement elem = new RecommendedElement(
							tokens[0], tokens[3], Integer.parseInt(tokens[5]),
							Integer.parseInt(tokens[7]));

					if (!courses.contains(tokens[3])) {
						courses.add(tokens[3]);
						recommended.add(elem);
					}
				}
			} finally {
				input.close();
				output.close();
			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}

	}

	public static List<RecommendedElement> sortElements(
			final List<RecommendedElement> elements) {
		Collections.sort(elements, new Comparator<RecommendedElement>() {
			public int compare(final RecommendedElement elem1,
					final RecommendedElement elem2) {

				int value1, value2;

				if (elem1.getCourseCode().contains("(")) {
					value1 = 1000 + elem1.getCourseRank();
				} else {
					value1 = elem1.getUnit();
				}

				if (elem2.getCourseCode().contains("(")) {
					value2 = 1000 + elem2.getCourseRank();
				} else {
					value2 = elem2.getUnit();
				}

				return value1 - value2;
			}
		});

		return elements;
	}
}
